<?php

App::uses('AppController', 'Controller');

/**
 * Coupleds Controller
 *
 * @property Coupled $Coupled
 */
class CoupledsController extends AppController {

  function beforeFilter() {
    $this->Auth->allow('selectChange');
    $this->set('title_for_layout', 'Acoplados');
  }

  public function selectChange() {
    $this->set('coupleds', $this->getCoupledsList($this->request->data('idCompany')));
    $this->layout = 'ajax';
  }

  public function getCoupledsList($idCompany = null) {

    if (isset($idCompany)) {
      $conditions = array('Coupled.company_id' => $idCompany);
    } else {
      $conditions = array();
    }
    $Coupleds = $this->Coupled->find('all', array(
        'order' => array(
            'Coupled.model ASC',
            'Coupled.domain ASC'
        ),
        'fields' => array(
            'Coupled.id',
            'Coupled.model',
            'Coupled.domain'
        ),
        'conditions' => $conditions,
        'recursive' => 0
            ));

    $CoupledsList = array();
    foreach ($Coupleds as $Coupled) {
      $CoupledsList[$Coupled['Coupled']['id']] = $Coupled['Coupled']['model'] . ' - ' . $Coupled['Coupled']['domain'];
    }
    return $CoupledsList;
  }

  public function selectOne() {

    $this->loadModel('Driver');
    $coupleds = $this->Driver->find('first', array('fields' => array('Driver.coupled_id'), 'conditions' => array('Driver.id' => $this->request->data('idDriver'))));
    $coupledId = $coupleds['Driver']['coupled_id'];
    $this->set('coupleds', $this->getCoupledsListOne($coupledId));
    $this->layout = 'ajax';
  }

  public function getCoupledsListOne($coupledId = null) {

    if (isset($coupledId)) {
      $conditions = array('Coupled.id' => $coupledId);
    } else {
      $conditions = array();
    }
    $Coupleds = $this->Coupled->find('all', array(
        'order' => array(
            'Coupled.model ASC',
            'Coupled.domain ASC'
        ),
        'fields' => array(
            'Coupled.id',
            'Coupled.model',
            'Coupled.domain'
        ),
        'conditions' => $conditions,
        'recursive' => 0
            ));

    $CoupledsList = array();
    foreach ($Coupleds as $Coupled) {
      $CoupledsList[$Coupled['Coupled']['id']] = $Coupled['Coupled']['model'] . ' - ' . $Coupled['Coupled']['domain'];
    }
    return $CoupledsList;
  }

  /**
   * index method
   *
   * @return void
   */
  public function index() {
    $this->Coupled->recursive = 0;
    $this->paginate = array('order' => 'Coupled.id desc', 'limit' => 50);
    $this->set('coupleds', $this->paginate());
  }

//  search by Transport(Company name)
  public function search() {
    $name = '%' . trim($this->request->data('name')) . '%';

    $this->paginate = array(
        'conditions' => array('OR' => array(
                'Company.social_razon LIKE' => $name,
                'Coupled.driver LIKE' => $name
        )));

    $this->set('coupleds', $this->paginate());
    $this->layout = 'ajax';
  }

  public function indexAjax() {
    $this->Coupled->recursive = 0;
    $this->paginate = array('order' => 'Coupled.id desc', 'limit' => 50);
    $this->set('coupleds', $this->paginate());
    $this->layout = 'ajax';
  }

  /**
   * view method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function view($id = null) {
    if (!$this->Coupled->exists($id)) {
      throw new NotFoundException(__('Invalid coupled'));
    }
    $options = array('conditions' => array('Coupled.' . $this->Coupled->primaryKey => $id));
    $this->set('coupled', $this->Coupled->find('first', $options));
  }

  /**
   * add method
   *
   * @return void
   */
  public function add() {
    if ($this->request->is('post')) {
      $this->Coupled->create();
      if ($this->Coupled->save($this->request->data)) {
        $this->Session->setFlash(__('El acoplado ha sido creado exitosamente'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('El acoplado NO ha sido creado'));
      }
    }
    $companies = $this->Coupled->Company->find('list');
    $this->set(compact('companies'));
  }

  public function addAll($model = null, $domain = null, $company_id = null) {

    // Creo los datos en un array        
    $data = array(
        'Coupled' => array('model' => $model, 'domain' => $domain, 'company_id' => $company_id)
    );
    $this->Coupled->create();
    if ($this->Coupled->save($data)) {
      return $this->Coupled->getId();
    }
  }

  /**
   * Ajax add method
   *
   * @return void
   */
  public function addAjax() {
    if ($this->request->is('post')) {
      $this->Coupled->create();
      if ($this->Coupled->save($this->request->data)) {
        $this->Session->setFlash(__('El acoplado ha sido creado exitosamente'));
        $this->redirect('/');
      } else {
        $this->Session->setFlash(__('El acoplado NO ha sido creado'));
      }
    }
    $companies = $this->Coupled->Company->find('list');
    $this->set(compact('companies'));
    $this->layout = 'ajax';
  }

  /**
   * edit method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function edit($id = null) {

    $this->Coupled->id = $id;


    if (!$this->Coupled->exists($id)) {
      throw new NotFoundException(__('Invalid coupled'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
      if ($this->Coupled->save($this->request->data)) {
        $this->Session->setFlash(__('El Acoplado ha sido Guardado exitosamente'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('El Acoplado No ha sido Guardado'));
      }
    } else {
      $options = array('conditions' => array('Coupled.' . $this->Coupled->primaryKey => $id));
      $this->request->data = $this->Coupled->find('first', $options);
    }
    $companies = $this->Coupled->Company->find('list');
    $this->set(compact('companies'));
  }

  /**
   * delete method
   *
   * @throws NotFoundException
   * @throws MethodNotAllowedException
   * @param string $id
   * @return void
   */
  public function delete($id = null) {
    $this->Coupled->id = $id;
    if (!$this->Coupled->exists()) {
      throw new NotFoundException(__('Invalid coupled'));
    }
    $this->request->onlyAllow('post', 'delete');
    if ($this->Coupled->delete()) {
      $this->Session->setFlash(__('El acoplado ha sido BORRADO exitosamente'));
      $this->redirect(array('action' => 'index'));
    }
    $this->Session->setFlash(__('El acoplado NO ha sido creado exitosamente'));
    $this->redirect('/');
  }

}
